package com.hgkj.cloud.customer.controller;

import com.hgkj.cloud.base.common.PageInfo;
import com.hgkj.cloud.base.domain.TenantConsult;
import com.hgkj.cloud.base.domain.TenantConsultDetail;
import com.hgkj.cloud.base.service.TenantConsultDetailService;
import com.hgkj.cloud.base.service.TenantConsultService;
import com.hgkj.cloud.customer.common.ResponseData;
import com.hgkj.cloud.base.domain.AdminUser;
import com.hgkj.cloud.base.domain.EUser;
import com.hgkj.cloud.base.domain.ExtPsychologist;
import com.hgkj.cloud.base.service.AdminUserService;
import com.hgkj.cloud.base.service.ExtPsychologistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/consult")
public class ConsultController extends BaseController {

    @Autowired
    private TenantConsultService tenantConsultService;
    @Autowired
    private TenantConsultDetailService tenantConsultDetailService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private ExtPsychologistService extPsychologistService;
    /**
     * 咨询师回复留言
     *
     * @param request
     * @param description 内容
     * @param consultId   留言id
     * @return
     */
    @RequestMapping("/savePsyConsultForMB")
    @ResponseBody
    public ResponseData savePsyConsultForMB(HttpServletRequest request, String description, Long consultId) {
        String opTime = new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date());
        ResponseData mResponseData = new ResponseData();
        Map<String, Object> p_map = new HashMap<String, Object>();
        if (consultId == null) {
            mResponseData.setCode(-1);
            mResponseData.setMsg("咨询id为空");
            return mResponseData;
        }
        TenantConsult consult = tenantConsultService.selectByPrimaryKey(consultId);
        try {
            consult.setIsRead(0);
            consult.setTitle(description);
            tenantConsultService.updateByPrimaryKeySelective(consult);
            TenantConsultDetail mConsultDetail = new TenantConsultDetail();
            mConsultDetail.setType(1);
            mConsultDetail.setConsultId(consultId);
            mConsultDetail.setDescription(description);
            tenantConsultDetailService.insertConsultDetail(mConsultDetail);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user_uuid", consult.getUserUuid());
            map.put("psychologist_id", consult.getPsychologistId());
            List<TenantConsultDetail> mList = tenantConsultDetailService.listConsultDetailById(map);
            map.put("list", mList);
            mResponseData.setData(map);
            mResponseData.setMsg("回复成功");
        } catch (Exception e) {
            mResponseData.setCode(-1);
            mResponseData.setMsg("回复失败");
            e.printStackTrace();
        }
        return mResponseData;
    }

    /**
     * 个人回复留言
     *
     * @param request
     * @param description     内容
     * @param psychologist_id 咨询师id
     * @return
     */
    @RequestMapping("/saveConsultForMB")
    @ResponseBody
    public ResponseData saveConsultForMB(HttpServletRequest request, String description, String user_uuid, String psychologist_id) {
        ResponseData mResponseData = new ResponseData();
        Long consult_id = null;

        Map<String, Object> p_map = new HashMap<String, Object>();
        EUser operator = getUserInfoByUuid(user_uuid);
        if (operator == null) {
            mResponseData.setCode(-1);
            mResponseData.setMsg("普通用户查询失败");
            return mResponseData;
        }
        try {
            TenantConsult consult = null;
            p_map.clear();
            p_map.put("user_uuid", user_uuid);
            p_map.put("psychologist_id", psychologist_id);
            List<TenantConsultDetail> list = tenantConsultDetailService.listConsultDetailById(p_map);
            if (list == null || list.size() == 0) {
                consult = new TenantConsult();
                consult.setTitle(description);
                consult.setFrameworkId(operator.getFramework_id());
                consult.setChannelId(operator.getChannel_id());
                consult.setUserUuid(operator.getUuid());
                consult.setPsychologistId(psychologist_id);
                tenantConsultService.insertSelective(consult);
                consult_id = consult.getId();
            } else {
                consult_id = list.get(0).getConsultId();
                consult = new TenantConsult();
                consult.setId(consult_id);
                consult.setTitle(description);
                tenantConsultService.updateByPrimaryKeySelective(consult);
            }
            TenantConsultDetail record = new TenantConsultDetail();
            record.setConsultId(consult_id);
            record.setDescription(description);
            record.setType(0);
            tenantConsultDetailService.insertConsultDetail(record);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user_uuid", user_uuid);
            map.put("psychologist_id", psychologist_id);
            List<TenantConsultDetail> mList = tenantConsultDetailService.listConsultDetailById(map);
            map.put("list", mList);
            mResponseData.setData(map);
            mResponseData.setMsg("留言成功");
        } catch (Exception e) {
            // log.debug(e.getMessage());
            mResponseData.setCode(-1);
            mResponseData.setMsg("留言失败");
            e.printStackTrace();
            return mResponseData;
        }

        return mResponseData;
    }
    /**
     * 获取普通用户所在单位及以上咨询师列表和其对话记录
     *
     * @param user_uuid 用户id
     * @return
     */
    @RequestMapping(value = "/getConsultListForUserMB")
    @ResponseBody
    public ResponseData getPsyInfoListForMB(String user_uuid, String psy_name) {
        Map<String, Object> map = new HashMap<String, Object>();
        ResponseData mResponseData = new ResponseData();
        EUser operator = getUserInfoByUuid(user_uuid);
        List<Long> framework_ids = new ArrayList<>();
        if (operator.getFramework_id() != null) {
            List<Long> allTenantFrameworksId = getAllFathersTenantFrameworksId(operator.getFramework_id());
            framework_ids.add(operator.getFramework_id());
            if (allTenantFrameworksId != null)
                framework_ids.addAll(allTenantFrameworksId);
            map.put("framework_ids", framework_ids);
        }
        if (psy_name != null && !"".equals(psy_name))
            map.put("real_name", psy_name);
        map.put("user_type", 2);

        List<AdminUser> list = adminUserService.listAllAdminUsersByMap(map);
        for (int i = 0; i < list.size(); i++) {
            ExtPsychologist psy = new ExtPsychologist();
            psy = extPsychologistService.selectExtPsyInfoByUserUuid(list.get(i).getUuid());
            if (psy != null) {
                list.get(i).setGood_at(psy.getGood_at());
            } else {
                list.get(i).setGood_at("未设置");
            }
            if (list.get(i).getUuid() == operator.getUuid()) {
                AdminUser usertemp = new AdminUser();
                usertemp = list.get(0);
                list.set(0, list.get(i));
                list.set(i, usertemp);
            }
        }
        map.clear();
        map.put("list", list);
        mResponseData.setData(map);
        return mResponseData;
    }
    /**
     * 所有留言咨询列表
     *
     * @param request
     * @param page  `
     * @param pageSize
     * @param psychologist_id
     * @return
     */
    @RequestMapping("/consultListForMB")
    @ResponseBody
    public ResponseData consultListForMB(HttpServletRequest request, Integer page, Integer pageSize,
                                         String psychologist_id, Long framework_id, @RequestParam String admin_uuid,String searchName) {
        PageInfo pi = new PageInfo();
        ResponseData mResponseData = new ResponseData();
        Map<String, Object> map;
        AdminUser operator = getAdminUserInfoByUuid(admin_uuid);
        try {
            map = new HashMap<String, Object>();
            if (searchName != null && !"".equals(searchName)){
                map.put("searchName",searchName);
            }


            List<Long> framework_ids = new ArrayList<>();
            if (psychologist_id == null || "".equals(psychologist_id)) {
                if (!"TOP".equals(operator.getChannel_id())) {
                    if (framework_id == null) {
                        framework_id = operator.getFramework_id();
                    }
//                    List<Long> allTenantFrameworksId = getAllSonTenantFrameworksId(framework_id);
//                    allTenantFrameworksId.add(framework_id);
//                    map.put("framework_ids", allTenantFrameworksId);
                }
                getTreePathDatas(framework_id, map);
            } else {
                map.put("psychologist_id", psychologist_id);
            }
            if (page > 0) {
                pi.setCurrentPage(page);
                pi.setPageSize(pageSize);
            }
            List<Map<String, Object>> list = tenantConsultService.listConsultNew(pi, map);
            long count = tenantConsultService.getListConsultCountNew(map);
            map.clear();
            map.put("list", list);
            map.put("count", count);
            mResponseData.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            mResponseData.setCode(-1);
            mResponseData.setMsg("获取失败");
        }
        return mResponseData;
    }

    /**
     * 留言咨询详情页面
     *
     * @param request
     * @param user_uuid
     * @return
     */
    @RequestMapping("/gotoLookConsultForMB")
    @ResponseBody
    public ResponseData gotoLookConsultForMB(HttpServletRequest request, String user_uuid, String psychologist_id) {
        ResponseData mResponseData = new ResponseData();
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user_uuid", user_uuid);
            map.put("psychologist_id", psychologist_id);
            List<TenantConsultDetail> list = tenantConsultDetailService.listConsultDetailById(map);
            map.clear();
            map.put("consultDetail", list);
            mResponseData.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            mResponseData.setCode(-1);
            mResponseData.setMsg("获取失败");
        }
        return mResponseData;
    }
}
